package com.shuqi.database.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Context;
import android.text.TextUtils;

import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.QueryBuilder;
import com.shuqi.application.ShuqiApplication;
import com.shuqi.database.dao.OrmBaseDao;
import com.shuqi.database.model.BookBagCatalog;
import com.shuqi.model.bean.BookContentInfo;

/**
 * 
 * @author yaozhen
 * 
 */
public class BookBagCatalogDao extends OrmBaseDao {

    private static BookBagCatalogDao mInstance;

    private RuntimeExceptionDao<BookBagCatalog, Integer> mDao = ShuqiDatabaseHelper.getHelper(
            ShuqiApplication.getContext()).getRuntimeExceptionDao(BookBagCatalog.class);

    private BookBagCatalogDao(Context context) {

    }

    public static synchronized BookBagCatalogDao getInstance() {
        if (mInstance == null) {
            mInstance = new BookBagCatalogDao(ShuqiApplication.getContext());
        }
        return mInstance;
    }

    /**
     * 保存BookBagCatalogList 插入之前删除之前的数据
     * 
     * @return
     */
    public int saveBookBagCatalogList(final List<BookContentInfo> list, final String bookId) {
        Callable<Void> callble = new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                DeleteBuilder<BookBagCatalog, Integer> deleteBuilder = mDao.deleteBuilder();
                deleteBuilder.where().eq("book_id", bookId);
                deleteBuilder.delete();
                for (BookContentInfo info : list) {
                    BookBagCatalog bookBagCatalog = new BookBagCatalog();
                    bookBagCatalog.setBookId(info.getBookId());
                    bookBagCatalog.setBookBagFilePath(info.getFliePath());
                    bookBagCatalog.setChapterName(info.getCurChapterName());
                    bookBagCatalog.setChapterFileName(info.getChapterpath());
                    try {
                        bookBagCatalog.setChapterId(Integer.parseInt(info.getCurChapterCid()));
                        mDao.create(bookBagCatalog);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        };
        try {
            openTransactionManager(ShuqiDatabaseHelper.getHelper(ShuqiApplication.getContext()),
                    callble);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 根据bookId 查询出目录list
     * 
     * @return
     */
    public List<BookContentInfo> getBookBagCatalogList(String bookId) {
        if (TextUtils.isEmpty(bookId)) {
            return null;
        }

        QueryBuilder<BookBagCatalog, Integer> queryBuilder = mDao.queryBuilder();
        try {
            queryBuilder.where().eq("book_id", bookId);
            queryBuilder.orderBy("chapter_id", true);
            List<BookBagCatalog> list = queryBuilder.query();
            List<BookContentInfo> resultList = new ArrayList<BookContentInfo>();
            for (BookBagCatalog bookBag : list) {
                BookContentInfo info = new BookContentInfo();
                info.setFliePath(bookBag.getBookBagFilePath());
                info.setBookId(bookBag.getBookId());
                info.setCurChapterCid(bookBag.getChapterId() + "");
                info.setCurChapterName(bookBag.getChapterName());
                info.setChapterpath(bookBag.getChapterFileName());
                resultList.add(info);
            }
            return resultList;
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }

    /**
     * 根据bookId删除章节
     * 
     * @param bookId
     * @return
     */
    public int deleteCatalogByBagBookId(String bookId) {
        if (TextUtils.isEmpty(bookId)) {
            return -1;
        }
        DeleteBuilder<BookBagCatalog, Integer> deleteBuilder = mDao.deleteBuilder();
        try {
            deleteBuilder.where().eq("book_id", bookId);
            return deleteBuilder.delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return -1;
    }

    /**
     * 删除所有章节
     * 
     * @return
     */
    public int deleteAllCatalog() {
        try {
            return mDao.deleteBuilder().delete();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return -1;
    }

}
